package com.itdianzi.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itdianzi.dao.system.UserDao;
import com.itdianzi.domain.system.User;
import com.itdianzi.export_common.common.utils.Encrypt;
import com.itdianzi.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public PageInfo findAll(Integer page, Integer size,String companyId) {
        PageHelper.startPage(page, size);
        List<User> userList = userDao.findAll(companyId);

        /*System.out.println(userList);*/


        PageInfo pageInfo = new PageInfo(userList);
        return pageInfo;
    }

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }


    @Override
    public void save(User user) {


        String email = user.getEmail();
        String password = user.getPassword();
        String md5pw = Encrypt.md5(password, email);


        user.setPassword(md5pw);

        user.setId(UUID.randomUUID().toString());

        /*System.out.println(user);*/
        userDao.save(user);
    }

    @Override
    public void update(User user)
    {
        String email = user.getEmail();
        String password = user.getPassword();
        String md5pw = Encrypt.md5(password, email);

       /* System.out.println(email);
        System.out.println(password);
        System.out.println(md5pw);*/

        user.setPassword(md5pw);

        user.setId(UUID.randomUUID().toString());
        userDao.update(user);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void delete(String id) {
        userDao.delete(id);

    }

    @Override
    public void updateUserRole(String userid, String roleIds) {
        userDao.deleteUserRoleByUid(userid);
        //添加新的数据
        String[] splitRoleIds = roleIds.split(",");
        for (String splitRoleId : splitRoleIds) {
        userDao.saveUserRole(userid, splitRoleId);

        }

    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public void updateByEmail(User user) {
        String email = user.getEmail();
        String password = user.getPassword();
        String md5pw = Encrypt.md5(password, email);

       /* System.out.println(email);
        System.out.println(password);
        System.out.println(md5pw);*/

        user.setPassword(md5pw);

        userDao.updateByEmail(user);
    }
}
